﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    /// <summary>
    /// 实体转SQL
    /// </summary>
    public class ModelToSql
    {

        /// <summary>
        /// 将model转换为sqlparameter
        /// </summary>
        /// <typeparam name="T">泛型model的类型</typeparam>
        /// <param name="Model">实体</param>
        /// <param name="sql">输出参数，生成插入sql语句</param>
        /// <param name="table">表名</param> 
        /// <returns></returns>
        public static SqlParameter[] GetParameter<T>(T Model, ref string sql, string table)
        {

            if (string.IsNullOrEmpty(table))
            {
                return new SqlParameter[0];
            }
            Type t = Model.GetType();
            FieldInfo[] fis = t.GetFields();

            List<SqlParameter> list = new List<SqlParameter>();
            string tableName = "";
            string tableParameter = "";

            foreach (FieldInfo fi in fis)
            {
                object value = fi.GetValue(Model);
                string Name = GetSingleInfo<T>(fi, Model);
                if ("" != Name)
                {
                    tableName += Name + ",";
                    tableParameter += "@" + Name + ",";
                    list.Add(new SqlParameter(Name, value));
                }

            }

            SqlParameter[] sq = list.ToArray();
            sql = string.Format("insert into " + table + " ({0}) values ({1})", tableName.TrimEnd(','), tableParameter.TrimEnd(','));
            return sq;
        }

        //处理单个实体的信息  判断空
        public static string GetSingleInfo<T>(FieldInfo fi, T Model)
        {
            object value = fi.GetValue(Model);
            if (value != null)
            {
                string type = fi.GetValue(Model).GetType().Name;
                //判断时间是否为空
                if (type == "DateTime" && value + "" == "0001/1/1 0:00:00")
                {
                    return "";
                }
                //判断数字不能为0
                if (type == "Int32" && value + "" == "0")
                {
                    return "";
                }
                string name = fi.Name.ToLower();
                if ("id" != name)
                {
                    return name;
                }
            }
            return "";
        }
        /// <summary>
        /// 将model转换为sqlparameter
        /// </summary>
        /// <typeparam name="T">泛型model的类型</typeparam>
        /// <param name="Model">实体</param>
        /// <param name="sql">输出参数，生成插入sql语句</param>
        /// <param name="table">表名</param> 
        /// <param name="table">表主键</param> 
        /// <returns></returns>
        public static SqlParameter[] GetParameter<T>(T Model, ref string sql, string table, string tablePK)
        {
            if (string.IsNullOrEmpty(table))
            {
                return new SqlParameter[0];
            }
            Type t = Model.GetType();
            PropertyInfo[] propertys = t.GetProperties();

            List<SqlParameter> list = new List<SqlParameter>();
            string tableName = "";
            string tableParameter = "";

        
            foreach (PropertyInfo pi in propertys)
            {
                string Name = pi.Name;
                object value = pi.GetValue(Model);
                if ("" != Name)
                {
                    if (Name== "tableName"||Name=="id")
                    {
                        continue;
                    }
                    tableName += Name + ",";
                    tableParameter += "@" + Name + ",";
                    list.Add(new SqlParameter(Name, value));
                }
            }

            SqlParameter[] sq = list.ToArray();
            sql = string.Format("insert into " + table + " ({0}) values ({1})", tableName.TrimEnd(','), tableParameter.TrimEnd(','));
            return sq;
        }


        /// <summary>
        /// 将model转换为sqlparameter
        /// </summary>
        /// <typeparam name="T">泛型model的类型</typeparam>
        /// <param name="Model">实体</param>
        /// <param name="sql">输出参数，生成更新sql语句</param>
        /// <param name="table"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public static SqlParameter[] GetUpdateParameter<T>(T Model, ref string sql, string table, string queryid)
        {
            if (string.IsNullOrEmpty(table))
            {
                return new SqlParameter[0];
            }

            Type t = Model.GetType();
            //FieldInfo[] fis = t.GetFields();
            PropertyInfo[] propertys = t.GetProperties();

            List<SqlParameter> list = new List<SqlParameter>();

            string updateParameter = "";


            //foreach (FieldInfo fi in fis)
            //{
            foreach (PropertyInfo pi in propertys)
            {
                object value = pi.GetValue(Model);
                string Name = "";
                //根据那个字段进行更改
                if (queryid == pi.Name)
                {
                    Name = pi.Name;
                    list.Add(new SqlParameter(Name, value));
                    continue;
                }
                else
                {
                    //Name = GetSingleInfo<T>(pi, Model);
                    Name = pi.Name;
                }
                if ("" != Name)
                {
                    updateParameter += Name + "=@" + Name + ",";
                    list.Add(new SqlParameter(Name, value));
                }
            }
            SqlParameter[] sq = list.ToArray();
            sql = string.Format("update " + table + " set {0} where " + queryid + "=@" + queryid, updateParameter.TrimEnd(','));
            return sq;
        }


    }
}
